package cn.zhaoyuening.hadoopdemo.test;

import com.sun.xml.internal.xsom.impl.util.Uri;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.Test;

import java.io.InputStream;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.Arrays;

import static java.lang.System.in;

public class SplitDemo {
    private static Logger logger = Logger.getLogger(SplitDemo.class);
    private static String data = "010010 99999  20100115    35.8 24    34.1 24  1001.5 24  1000.3 24    4.3  6   11.3 24   17.5  999.9    36.5    35.1   0.00G 999.9  010000\n";
    @Test
    public void testSplitFormat() {
        String[] dataArr = data.split("\\s+");
        System.out.println(Arrays.toString(dataArr));
        System.out.println(dataArr.length);
        System.out.println(dataArr[17]);
        System.out.println(dataArr[18]);
        System.out.println(getFormatTemperature(dataArr[18]));

    }

    public float getFormatTemperature(String strVal){
        if (strVal.charAt(strVal.length()-1)=='*'){
            strVal = strVal.substring(0,strVal.length()-1);
        }
        return Float.valueOf(strVal);
    }

    @Test
    public void fileSystemApi() throws Exception {
        String url = "hdfs://192.168.0.100:9000/user/hadoop/jobs/maxTem/result/4/part-r-00000";
        Configuration conf = new Configuration();
        FileSystem fileSystem = FileSystem.get(URI.create(url), conf);
        String defaultName = conf.get("fs.default.name");
        System.out.println(defaultName);
        InputStream in = fileSystem.open(new Path(url));
        IOUtils.copyBytes(in,System.out,4096,true);
        fileSystem.close();
        Assert.assertTrue(true);
    }

    @Test
    public void fileSystemApiReadFull() throws Exception {
        String url = "hdfs://192.168.0.100:9000/user/hadoop/jobs/maxTem/result/4/part-r-00000";
        Configuration conf = new Configuration();
        FileSystem fileSystem = FileSystem.get(URI.create(url), conf);
        String defaultName = conf.get("fs.default.name");
        System.out.println(defaultName);

        FSDataInputStream in = fileSystem.open(new Path(url));
        byte[] buf = new byte[1024];
        in.readFully(buf);
        fileSystem.close();
        Assert.assertTrue(true);
        System.out.println(new String(buf));
    }

    @Test
    public void fileSystemApiWriteData() throws Exception {
        String url = "hdfs://192.168.0.100:9000";
        Configuration conf = new Configuration();
        FileSystem fileSystem = FileSystem.get(URI.create(url), conf,"hadoop");
        FSDataOutputStream out = fileSystem.create(new Path(url,"/user/hadoop/demo.txt"));
        out.write("hello".getBytes(Charset.forName("utf-8")));
        out.flush();
        out.close();
        fileSystem.close();
    }
}
